<?php
/**
 * Created by PhpStorm.
 * User: fanyongsheng
 * Date: 2017/5/21
 * Time: 20:59
 */

namespace Home\Controller;

use Think\Controller;

class ExcelController extends Controller{
    /**
     * 导出excell
     * @param $expTitle
     * @param $expCellName
     * @param $expTableData
     */
    private function exportExcel($expTitle,$expCellName,$expTableData){

        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
        $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
//  $objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1'); //合并单元格
        for ($i = 0; $i < $cellNum; $i++) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2), $expTableData[$i][$expCellName[$j][0]]);
            }
        }

        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        echo $objWriter->save('php://output');
        exit;
    }


    /**
     * 导出getData
     */
    public function getData(){
        $expTitile = $_POST['expTitle'];
        $expCellName = $_POST['expCellName'];
        $expTableData = $_POST['expTableData'];
        $expCellName = json_decode($expCellName,true);
        $expTableData = json_decode($expTableData,true);

//        $newData = array();
//        foreach($data as $key=>$value){
//            $newData[$key]['m_account'] =  $value['member']['m_account'];
//            $newData[$key]['a_money'] = $value['a_money'];
//            $newData[$key]['m_money'] = $value['member']['m_money'];
//            $newData[$key]['a_date_format'] = $value['a_date_format'];
//        }
////		$expCellName = array('账号','提现金额','剩余金额','提现日期');
//        $expCellName = array(array('m_account','账号'),array('a_money','提现金额'),array('m_money','剩余金额'),array('a_date_format','提现日期'));

        $this->exportExcel($expTitile,$expCellName,$expTableData);
    }

    /**
     *
     * test
     */

    public function test(){
        $expCellName = array(array('m_account','账号'),array('a_money','提现金额'),array('m_money','剩余金额'),array('a_date_format','提现日期'));
        echo json_encode($expCellName);
//        returnMobileJson(200,'fanhui',$expCellName);
    }




}